package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.service.entity.pojo.ChiliStudy;
import com.spice.service.entity.pojo.FlowerChiliStudy;
import com.spice.service.mapper.FlowerChiliStudyMapper;
import com.spice.service.service.FlowerChiliStudyService;
import org.springframework.stereotype.Service;

@Service
public class FlowerChiliStudyServiceImpl extends ServiceImpl<FlowerChiliStudyMapper, FlowerChiliStudy> implements FlowerChiliStudyService {
    @Override
    public Response saveFlowerChiliStudy(FlowerChiliStudy newFlowerChiliStudy) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(
                newFlowerChiliStudy.getChineseName(),
                newFlowerChiliStudy.getOrderId(),
                newFlowerChiliStudy.getMaintainer()
        ))
        {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //检查序号是否重复
        if (this.count(new LambdaQueryWrapper<FlowerChiliStudy>().eq(FlowerChiliStudy::getOrderId,newFlowerChiliStudy.getOrderId())) > 0)
        {
            throw new SpiceException().message("序号与已有冲突");
        }
        //检查中文名称是否重复
        if (this.count(new LambdaQueryWrapper<FlowerChiliStudy>().eq(FlowerChiliStudy::getChineseName,newFlowerChiliStudy.getChineseName())) > 0)
        {
            throw new SpiceException().message("中文名称与已有冲突");
        }
        this.save(newFlowerChiliStudy);
        return Response.ok().message("花椒科研信息添加成功").data("data",newFlowerChiliStudy);
    }

    @Override
    public Response removeFlowerChiliStudy(Long id) {
        if(CheckUtil.objectsHasNull(id))
        {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认待删除数据是否存在
        FlowerChiliStudy removedFlowerChiliStudy = this.getById(id);
        if (CheckUtil.objectsHasNull(removedFlowerChiliStudy))
        {
            throw new SpiceException().message("该花椒科研数据不存在");
        }
        this.removeById(id);
        return Response.ok().message("花椒科研数据删除成功").data("data",removedFlowerChiliStudy);
    }

    @Override
    public Response updateFlowerChiliStudy(Long id, FlowerChiliStudy newFlowerChiliStudy) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(id, newFlowerChiliStudy)) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认更新的是否存在
        FlowerChiliStudy updatedFlowerChiliStudy = this.getById(id);
        if (CheckUtil.objectsHasNull(updatedFlowerChiliStudy))
        {
            throw new SpiceException().message("该花椒科研数据不存在");
        }
        //确认更新后的序号是否与已有的冲突
        if (!newFlowerChiliStudy.getOrderId().equals(updatedFlowerChiliStudy.getOrderId()))
        {
            if (this.count(new LambdaQueryWrapper<FlowerChiliStudy>().eq(FlowerChiliStudy::getOrderId,newFlowerChiliStudy.getOrderId())) > 0)
            {
                throw new SpiceException().message("修改后的序号与已有序号冲突");
            }
        }
        //确认更新后的中文名称是否与已有的冲突
        if (!newFlowerChiliStudy.getChineseName().equals(updatedFlowerChiliStudy.getChineseName()))
        {
            if (this.count(new LambdaQueryWrapper<FlowerChiliStudy>().eq(FlowerChiliStudy::getChineseName,newFlowerChiliStudy.getChineseName())) > 0)
            {
                throw new SpiceException().message("修改后的中文名称与已有中文名称冲突");
            }
        }
        //更新待更新的数据的id和createTime
        newFlowerChiliStudy.setId(id);
        newFlowerChiliStudy.setCreateTime(updatedFlowerChiliStudy.getCreateTime());
        if (this.updateById(newFlowerChiliStudy)) {
            return Response.ok().message("花椒科研数据修改成功").data("data",newFlowerChiliStudy);
        }
        else {
            return Response.error().message("花椒科研数据修改失败");
        }
    }
}
